#include"head.h"
int main(int argc, const char *argv[])
{
	int i = 0;
	int flage;
	ssize_t sizebyte;

 sendEnv env;

	cmd.cmd_num = 3;
	cmd.value = 2;
	char buf;
	int fd = open("/dev/ttyUSB0", O_RDWR );
	if(fd < 0)
		exit(1);
	serial_init(fd);
	printf("open ok!\n");
	sendEnv cmdrcv;
	options.c_cc[VMIN] = sizeof(sendEnv);//zigbee接收的数据长度
	tcsetattr(fd,TCSANOW,&options);//tcsanow不等数据传出完毕立即更改属性  option 为参数
	/*
	while(1)
	{

		
	}
	
	*/
	while(1)
	{
		ssize_t byte = read(fd, &cmdrcv, sizeof(sendEnv));
		if(byte < 0)
		{
			tcflush(fd, TCIFLUSH);
			sleep(1);
			continue;
		}
	
		printf("%s %d \n", __func__, __LINE__ );
		printf("temp:%d.%d,  hum:%d.%d, x:%d, y:%d, z:%d light:%d,goods_flags:%d\n", \
				cmdrcv.temp[0], cmdrcv.temp[1], cmdrcv.hum[0], cmdrcv.hum[1],\
				cmdrcv.x, cmdrcv.y, cmdrcv.z, cmdrcv.light, cmdrcv.flag);
		if(cmdrcv.flag == 1)
		{

			cmd.value = 2;
			printf("%s  %d\n",__func__, __LINE__);
			write(fd, &cmd, sizeof(struct cmd_t));//发送命令编码 
			}
		if(cmdrcv.flag == 0)
		{
			cmd.value = 1;
			printf("%s  %d\n",__func__, __LINE__);
			write(fd, &cmd, sizeof(struct cmd_t));//发送命令编码 
		}
		bzero(&cmdrcv, sizeof(sendEnv));   
		usleep(200);
		usleep(300);
	}


	close(fd);

	return 0;
}




